package com.functest.jpwise.deprecated;

import com.functest.jpwise.algo.PairwiseAlgorithm;
import com.functest.jpwise.core.*;

import java.util.*;

/** Represents a factory that creates parameter combinations for a jUnit test in a pair-wise manner.
 * Using such a factory ensures, in the minimum number of combinations (i.e. test cases), that all possible pairings of
 * parameter values get tested. This is the most efficient form of orthogonal-array testing and is most useful in a
 * regression-test suite.
 * @author Jeremy Reeder
 * @version 2011-11-15
 * @since 2011-10-26 */
public class AllPairsParameterFactory extends OrthogonalParameterFactory {
	public AllPairsParameterFactory() {super(2); }
	protected List<ParameterValue[]> createListOfParameterIdCombinations() {
		assertSufficientParameters();
		final int parameterCount = orderedListOfParameterIdSets.size();
		final TestInput testInput = new TestInput();
		int dimensionIndex = 0;
        for (final Set<ParameterValue> inputIdSet : orderedListOfParameterIdSets) {
            final String dimensionId = Integer.toString(dimensionIndex);
            dimensionIndex++;

            final TestParameter testParameter = new TestParameter(dimensionId, new ArrayList(inputIdSet));
            testInput.add(testParameter);
        }
		final TestGenerator testGenerator = new TestGenerator(testInput);
		testGenerator.generate(new PairwiseAlgorithm(5), getDimensionCount());
		final CombinationTable combinationTable = testGenerator.result();
		final List<ParameterValue[]> listOfValueIdArrays = new ArrayList<>();
		for (Combination combination : combinationTable.combinations()) {
			final ParameterValue[] valueIds = combination.getValues();
			final Random random = new Random(0);
			for (int i = 0; i < parameterCount; i++) {
				if (valueIds[i] == null) {
					final Set<ParameterValue> valueIdSet = orderedListOfParameterIdSets.get(i);
					final List<ParameterValue> inputIds = new ArrayList(valueIdSet);
					final int randomValueIndex = random.nextInt(inputIds.size());
					valueIds[i] = inputIds.get(randomValueIndex);
				}
			}
			listOfValueIdArrays.add(valueIds);
		}
		return listOfValueIdArrays;
	}
}
